import dayjs from 'dayjs';

export default {

	saveLoginInfo: (loginInfo) => {
		uni.setStorageSync('__LOGIN_INFO_KEY', loginInfo)
	},
	clearLoginInfo: (loginInfo) => {
		uni.setStorageSync('__LOGIN_INFO_KEY', null)
	},
	loadLoginInfo: () => {
		return uni.getStorageSync('__LOGIN_INFO_KEY');
	},

	getLoginToken: () => {
		const loginInfo = uni.getStorageSync('__LOGIN_INFO_KEY');
		if (loginInfo && loginInfo.token) {
			return loginInfo.token;
		} else {
			return "";
		}
	},
	saveLoginToken: (token) => {
		const loginInfo = uni.getStorageSync('__LOGIN_INFO_KEY');
		if (loginInfo && loginInfo.token) {
			loginInfo.token = token;
			uni.setStorageSync('__LOGIN_INFO_KEY', loginInfo)
		}
	},
	getUserName: () => {
		const loginInfo = uni.getStorageSync('__LOGIN_INFO_KEY');
		if (loginInfo && loginInfo.userName) {
			return loginInfo.userName;
		} else {
			return "";
		}
	},
	getUserId: () => {
		const loginInfo = uni.getStorageSync('__LOGIN_INFO_KEY');
		if (loginInfo && loginInfo.userId) {
			return loginInfo.userId;
		} else {
			return "";
		}
	},
	getUserRoleList: () => {
		const loginInfo = uni.getStorageSync('__LOGIN_INFO_KEY');
		if (loginInfo && loginInfo.roleList) {
			return loginInfo.roleList;
		} else {
			return [];
		}
	},
	hasRole: (roleName) => {
		const loginInfo = uni.getStorageSync('__LOGIN_INFO_KEY');
		if (loginInfo && loginInfo.roleList) {
			return loginInfo.roleList.indexOf(roleName) >= 0;
		} else {
			return false;
		}
	},

	formatDate: (time, format) => {
		if (time) {
			if (!format) {
				format = "YYYY-MM-DD";
			}
			return dayjs(time).format(format);
		}
		return "";
	},
	formatTime: (time, format) => {
		if (time) {
			if (!format) {
				format = "YYYY-MM-DD HH:mm:ss";
			}
			return dayjs(time).format(format);
		}
		return ""
	},
	todayString: (format) => {
		if (!format) {
			format = "YYYY-MM-DD";
		}
		return dayjs().format(format);
	},
	yesterdayString: (format) => {
		if (!format) {
			format = "YYYY-MM-DD";
		}
		return dayjs().add(-1, 'day').format(format);
	},
	nowString: (format) => {
		if (!format) {
			format = "YYYY-MM-DD HH:mm:ss";
		}
		return dayjs().format(format);
	},
	timeString: (format) => {
		if (!format) {
			format = "HH:mm:ss";
		}
		return dayjs().format(format);
	},
	parseMultiLine(str) {
		return str.split('\n');
	},

	getWeekString: (time) => {
		if (time) {
			switch (dayjs(time).get('day')) {
				case 0:
					return '星期日';
				case 1:
					return '星期一';
				case 2:
					return '星期二';
				case 3:
					return '星期三';
				case 4:
					return '星期四';
				case 5:
					return '星期五';
				case 6:
					return '星期六';
				default:
					return '';
			}
		}
		return '';
	},

	checkInvokeSuccess: (ir) => {
		return ir !== undefined;
	},

	checkLogin: () => {
		if (getApp().globalData.isLogined === true) {

			return true;

		} else {

			const pages = getCurrentPages()
			const currentPage = pages[pages.length - 1]
			const fullPath = currentPage.$page.fullPath

			uni.redirectTo({
				url: '/pages/login/login?gotoUrl=' + encodeURIComponent(fullPath)
			})
		}
	},

	formatDate: (dateString) => {
		return dateString ? dateString.replace('T', ' ') : dateString;
	}
}